<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客编辑</title>

    <!-- 引入自己写的样式 -->
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/blog_edit.css">

    <!-- 引入 editor.md 的依赖 -->
    <link rel="stylesheet" href="editor.md/css/editormd.min.css"/>
    <script src="js/jquery.min.js"></script>
    <script src="editor.md/editormd.js"></script>
    <script src="js/common.js"></script>
</head>

<body>
<!-- 导航栏 -->
<div class="nav">
    <img src="img/logo2.jpg" alt="">
    <span class="title">博客系统</span>
    <!-- 用来占据中间位置 -->
    <span class="spacer"></span>
    <a href="blog_list.html">主页</a>
    <a href="myblog_list.html">我的文章</a>
    <!--    <a href="login.html">登陆</a>-->
    <a href="javascript:logout()">注销</a>
    <a href="about.html">站长简介</a>
</div>
<!-- 编辑框容器 -->
<div class="blog-edit-container">
    <!-- 标题编辑区 -->
    <div class="title">
        <input id="title" type="text" placeholder="在这里写下文章标题">
        <button onclick="mysub()">修改文章</button>
    </div>
    <!-- 创建编辑器标签 -->
    <div id="editorDiv">
        <textarea id="editor-markdown" style="display:none;"></textarea>
    </div>
</div>

<script>
    // 文章 id
    var id = "";
    var editor;

    function initEdit(md) {
        editor = editormd("editorDiv", {
            width: "100%",
            height: "calc(100% - 50px)",
            markdown: md,
            path: "editor.md/lib/",
            saveHTMLToTextarea: true
        });
    }

    function mysub() {
        var title = jQuery("#title");
        if (title.val() == "") {
            alert("请先输入标题！");
            title.focus();
            return;
        }
        if (editor.getValue() == "") {
            alert("请先输入正文！");
            return;
        }
        jQuery.ajax({
            url: "/article/update",
            type: "POST",
            data: {"id": id, "title": title.val(), "content": editor.getValue()},
            success: function (result) {
                if (result != null && result.code == 200 && result.data == 1) {
                    alert("修改成功！");
                    location.href = "myblog_list.html";
                } else {
                    alert("操作失败！");
                }
            }
        });
    }

    function initArt() {
        id = getUrlValue("id");
        if (id == "") {
            alert("无效参数");
            location.href = "myblog_list.html";
            return;
        }
        jQuery.ajax({
            url: "article/detail",
            type: "POST",
            data: {"id": id},
            success: function (result) {
                if (result != null && result.code == 200) {
                    jQuery("#title").val(result.data.title);
                    initEdit(result.data.content);
                } else {
                    alert("查询失败，请重试！");
                }
            }
        });
    }

    initArt();
</script>
</body>
</html>